package ro.utcn.oop.mandelbrot.gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.UIManager;


import ro.utcn.oop.mandelbrot.model.ImageDefs;
import ro.utcn.oop.mandelbrot.model.ImageService;

/**
 * Initialize and display application screen.
 * 
 */
// screnul principal, seteaza frame-ul, la asta dau ca mandelbrotscrenn sa fie continutul lui
public class ApplicationScreen {

	/** The main application frame. */
	private JFrame appFrame;

	/** The class instance. */
	private static ApplicationScreen instance;// single tone, instanta statica, metoda getinstance ca sa nu se creeze instante ale clasei respective

	/** A reference to the image service. */
	private ImageService imageService;

	/**
	 * The private constructor if the ApplicationScreen class. Sets the
	 * application look and feel to the system look and feel and calls the
	 * createGUI method.
	 */
	private ApplicationScreen() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());// face ca screenul sa aiba un look de windows
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.imageService = ImageService.getInstance(); //instantiez imageservice
		createGUI();
	}

	/**
	 * InitializeS the application frame.
	 */
	private void createGUI() { ///creeaza screenul aplicatiei, fara butoane...chenarul doar
		appFrame = new JFrame("Mandelbrot-Julia");
		appFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) { ///sa se apeleze metoda care inchide programul, ia eventul ca am inchis fereastra
				try {
					close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});

		MandelbrotScreen mandelbrotScreen = new MandelbrotScreen(); //instantiez mandelrotscreen
		appFrame.setJMenuBar(mandelbrotScreen.createMenuBar());//creez bara de meniu
		appFrame.setContentPane(mandelbrotScreen);//se seteaza continutul cu toolbarul, ala de sub menubar

		appFrame.setIconImage(imageService.getImage(ImageDefs.APPLICATION_ICON_PATH)); //se seteaa icon diin colt

		appFrame.setLocation(30, 30);//unde sa apara pe calc
		appFrame.pack();
		appFrame.setResizable(false);
		appFrame.setVisible(true);
	}

	
	/**
	 * Exits the application.
	 */
	public void close() {
		System.exit(0);
	}

	/**
	 * @return the instance of the class.
	 */
	public static ApplicationScreen getInstance() {  //returneaza instanta pt clasa
		if (instance == null) {
			instance = new ApplicationScreen();
		}
		return instance;
	}

}
